// PicnikExporterWindow.cs
//
//  Copyright (C) 2008 George Talusan
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
//

using FSpot.Extensions;
using FSpot;
using Gtk;
using Mono.Picnik.Core;
using Mono.Picnik.UI;
using System.IO;
using System.Net;
using System;

public partial class PicnikExporterWindow : Gtk.Window, IExporter
{	
	private Photo _photo;
	protected Photo Photo
	{
		get { return _photo; }
		set { _photo = value; }
	}
	
	private PicnikExporter _exporter;
	protected PicnikExporter Exporter
	{
		get { return _exporter; }
		set { _exporter = value; }
	}

	public PicnikExporterWindow (PicnikExporter exporter): base (Gtk.WindowType.Toplevel)
	{
		Exporter = exporter;
		Build ();
	}

	public void Run (IBrowsableCollection selection)
	{
		IBrowsableItem[] items = selection.Items;
		if (items.Length > 1)
		{
			PicnikExporterDialog dialog = new PicnikExporterDialog ();
			dialog.Run ();
			Hide ();
			return;
		}

		IBrowsableItem item = items[0];
		if (item is Photo)
		{
			Photo = item as Photo;
		}
		else if (item is PhotoVersion)
		{
			Photo = (item as PhotoVersion).Photo;
		}
		else
		{
			return;
		}
		
		Uri uri = item.DefaultVersionUri;
		string path = uri.LocalPath;
		
		Request request = new Request();
		request.Method = Constants.METHOD_POST;
		request.Import = path;
		request.Returntype = Constants.VALUE_RETURNTYPE_TEXT;
			
		request.ExportMethod = Constants.METHOD_GET;
		request.ExportAgent = Constants.VALUE_EXPORT_AGENT_BROWSER;
		request.Export = "http://localhost.fspot";
		request.ExportTitle = "Save to F-Spot";
		
		request.Exported += OnSaveToFSpot;
		
		view.Request = request;
		view.Render ();
	}
	
	private void OnSaveToFSpot (Request request)
	{
		uint[] ids = Photo.VersionIds;
		foreach (uint id in ids)
		{
			if (Photo.GetVersion (id).Name.Equals ("Picnik"))
			{
				Photo.DeleteVersion (id);
			}
		}
		uint versionId = Photo.CreateVersion ("Picnik", Photo.DefaultVersionId, true);
		IBrowsableItem version = Photo.GetVersion (versionId);
		Photo.DefaultVersionId = versionId;
		Stream stream = File.Open (version.DefaultVersionUri.LocalPath, FileMode.OpenOrCreate);
		stream.Write (request.ResponseAsBytes, 0, request.ResponseAsBytes.Length);
		stream.Close ();
		
		PhotoStore.GenerateThumbnail (version.DefaultVersionUri).Dispose ();
		Core.Database.Photos.Commit (Photo);

		Hide ();
		view.LoadUrl ("about:blank");
	}

	protected virtual void OnDeleteEvent (object o, Gtk.DeleteEventArgs args)
	{
		Hide ();
		view.LoadUrl ("about:blank");
		args.RetVal = true;
	}
}